
var getVote = function (postId) {
    //checkLogin(function () {
    $.ajax({
        url: baseurl + '/post/query/info.html',
        dataType: 'jsonp',
        data: {
            postId: postId,
            postType: 2
        }
    }).then(function (data) {
        ensureCorrectRes(data, function () {
            var html = template('vote_tpl', data.result);
            $('.vote-wrapper').html(html);
            Detail.getCommPraise(data.result);
            if (data.result.attendFlag == 0) {
                //未投票
                var mySwiper = new Swiper('.swiper-container', {
                    speed: 1000,
                    spaceBetween: 0,
                    autoplay: 3000,
                    onSlideChangeEnd: function (swiper) {
                        var page = $(swiper.container[0].parentElement.nextElementSibling);
                        var length = swiper.wrapper[0].childElementCount;
                        page.html(swiper.activeIndex + 1 + '/' + length);
                        if (!swiper.autoplaying) {
                            swiper.startAutoplay();
                        }
                    }
                });
                postVote(postId);
                onSelectChange();
            }
            else {
                //已投票
                computePercent()
            }

        })
    })
    // })
}

var postVote = function (postId) {

    $('button').click(function () {
        checkLogin(function () {
            var itemId = $('.vote-options').data('itemid'),
                optionId = $('input:checked').map(function (i, e) {
                    return $(e).data('optionid')
                }).toArray().toString();
            if (!optionId) {
                AlertBox({
                    type: 'mini',
                    msg: '请选择一个选项'
                })
                return;
            }
            $.ajax({
                url: baseurl + '/post/save/vote.do',
                dataType: 'jsonp',
                data: {
                    postId: postId,
                    itemId: itemId,
                    optionIds: optionId
                }
            }).then(function (data) {
                if (!data.resultCode) {
                    AlertBox({
                        msg: '投票成功',
                        type: 'mini',
                    })
                    setTimeout(function () {
                        location.reload()
                    }, 100)
                }
                else {
                    AlertBox({
                        msg: data.resultMsg,
                        type: 'mini',
                    })
                }
            })
        })
    })
}

var onSelectChange = function () {
    $('input').on('change', function (e) {
        $(this).closest('.clearfix').find('label').removeClass('selected')
            .end().find('input:checked').closest('label').addClass('selected')

    })
}

var computePercent = function () {
    var total = 0;
    $('.sel-con').find('.t-peo').each(function (i, v) {
        var cnt = parseInt($(v).text(), 10);
        total += cnt;
    }).each(function (i, v) {
        var cnt = parseInt($(v).text(), 10);
        var percent = cnt / total * 100 + '%';
        $(v).siblings('.t-bg').css('width', percent);

    })
}

$(function () {
    var postId = getUrlParam('postid') || 1;
    getVote(postId)
})